﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Smile.Mvvm.Service
{
    /// <summary>Service which manage operations with the application settings storage.</summary>
    public class SettingsService : ISettingsService
    {
        // @Indexer
        public object this[string key]
        {
            get
            {
                return _appSettings.Values[key];
            }
            set
            {
                lock (_locker)
                {
                    Add(key, value);
                }
            }
        }


        // @Public
        public void Add(string key, object value)
        {
            _appSettings.Values[key] = value;
        }

        public bool Contains(string key)
        {
            return _appSettings.Values.ContainsKey(key);
        }

        public void Remove(string key)
        {
            lock (_locker)
            _appSettings.Values.Remove(key);
        }

        public bool TryGetValue<T>(string key, out T value)
        {
            object obj = null;
            bool ret = _appSettings.Values.TryGetValue(key, out obj);
            value = (T)obj;
            return ret;
        }


        // @Private
        ApplicationDataContainer    _appSettings = ApplicationData.Current.LocalSettings;
        object                      _locker = new object();
    }
}
